// pages/report/report.ts
interface UploadFile {
  url: string;
}

Page({
  data: {
    reportCategory: '未知类别', // 默认值
    reportContent: '',
    fileList: [] as UploadFile[],
  },

  onLoad(options: { category?: string }) {
    // 从页面跳转的 query 中获取举报类别
    if (options.category) {
      this.setData({
        reportCategory: decodeURIComponent(options.category)
      });
    }
  },

  // afterRead 和 deleteFile 方法与 feedback.ts 中的完全相同，可以直接复用
  afterRead(event: WechatMiniprogram.CustomEvent) {
    const { file } = event.detail;
    this.data.fileList.push({ ...file, url: file.url });
    this.setData({ fileList: this.data.fileList });
  },

  deleteFile(event: WechatMiniprogram.CustomEvent) {
    const { index } = event.detail;
    this.data.fileList.splice(index, 1);
    this.setData({ fileList: this.data.fileList });
  },

  /**
   * 提交举报
   */
  async submitReport() {
    if (!this.data.reportContent.trim()) {
      wx.showToast({
        title: '举报内容不能为空',
        icon: 'none',
      });
      return;
    }

    wx.showLoading({ title: '提交中...', mask: true });

    try {
      // 同样，这里也需要模拟上传图片等异步操作
      // const imageUrls = await this.uploadImages(); // 可以复用 feedback 页的上传逻辑

      const reportData = {
        category: this.data.reportCategory,
        content: this.data.reportContent,
        // images: imageUrls,
      };

      console.log('正在提交举报数据:', reportData);
      await new Promise(resolve => setTimeout(resolve, 1500));

      wx.hideLoading();
      
      wx.showModal({
          title: '举报成功',
          content: '我们已收到您的举报，将会尽快核实处理。感谢您的监督！',
          showCancel: false,
          confirmText: '知道了',
          success: (res) => {
              if (res.confirm) {
                  wx.navigateBack(); // 提交成功后返回上一页
              }
          }
      })

    } catch (error) {
      wx.hideLoading();
      wx.showToast({ title: '提交失败，请重试', icon: 'none' });
    }
  },
});
